<template>
  <div class="hm-registered">
    <div class="title">面经注册</div>
    <div class="user">
      <span class="user-left">用户名</span>
      <input
        type="text"
        placeholder="用户名"
        class="user-right"
        v-model="user"
      />
    </div>
    <div class="password">
      <span class="password-left">密码</span>
      <input
        type="password"
        placeholder="密码"
        class="password-right"
        v-model="password"
      />
    </div>
    <van-button type="primary" size="large" color="#07c160" @click="register"
      >注册</van-button
    >
    <div class="log">
      <router-link to="/login" class="login">有账号,去登录</router-link>
    </div>
  </div>
</template>

<script>
//导入UserRequest
import { UserRequest } from "@/api/user";
export default {
  name: "HmRegister",
  data() {
    return {
      user: "",
      password: "",
    };
  },
  methods: {
    register() {
      const regex = /^[a-zA-Z0-9_-]{3,16}$/;
      const regex2 = /^[a-zA-Z0-9]{6,10}$/;
      if (this.user === "" || this.password === "") {
        this.$toast("请输入用户名或密码");
      } else if (!regex.test(this.user)) {
        this.$toast("用户名只能是字母数字下划线,长度3-16位");
      } else if (!regex2.test(this.password)) {
        this.$toast("密码只能是字母数字,长度6-10位");
      } else {
        //发送注册请求
        UserRequest.UserRegister({ user: this.user, password: this.password });
      }
    },
  },
};
</script>

<style scoped lang="less">
.hm-registered {
  padding: 10px;
  .title {
    font-size: (20 / @vw);
    text-align: center;
    margin-bottom: (10 / @vw);
  }
  .user {
    height: (44 / @vw);
    display: flex;
    align-items: center;
    font-size: (16 / @vw);
    //上下边框 实线 1px 颜色#ccc
    border-bottom: 1px solid #ccc;
    border-top: 1px solid #ccc;

    .user-left {
      width: (86 / @vw);
      height: (24 / @vw);
      color: #999;
    }
    .user-right {
      border: none;
      height: (24 / @vw);
    }
    .user-right::placeholder {
      font-size: (14 / @vw);
      color: #ccc;
    }
  }
  .password {
    height: (44 / @vw);
    display: flex;
    align-items: center;
    font-size: (16 / @vw);
    //上下边框 实线 1px 颜色#ccc
    border-bottom: 1px solid #ccc;
    margin-bottom: (10 / @vw);
    .password-left {
      width: (86 / @vw);
      height: (24 / @vw);
      color: #999;
    }
    .password-right {
      border: none;
      height: (24 / @vw);
    }
    .password-right::placeholder {
      font-size: (14 / @vw);
      color: #ccc;
    }
  }
  .log {
    display: flex;
    justify-content: right;
    margin-top: (20 / @vw);
    .login {
      font-size: (14 / @vw);
      text-decoration: none;
    }
  }
}
</style>
